<?php
/**
 * Created by PhpStorm.
 * User: zhangxilu
 * Date: 2018/2/27
 * Time: 下午11:08
 */

namespace common\logic;


use common\models\MessageModel;
use yii\db\ActiveQuery;

class MessageLogic extends Logic
{

    /**
     * @return ActiveQuery
     */
    protected function _getActiveQuery()
    {
        return MessageModel::find();
    }

    /**
     * 分页
     *
     * @param   int     $page
     * @param int    $pageSize
     * @param int  $id
     *
     * @return array
     */
    public function getByPage($id,$page,$pageSize = 20)
    {
        $nextPage = 0;
        $query = $this->_getActiveQuery();

        $offset = ($page - 1) * $pageSize;
        $query = $query->select(['id', 'title', 'content', 'create_at'])->limit($pageSize + 1)->where(['hid'=>$id])->offset($offset)->orderBy(['id'=>SORT_DESC]);


        $data = $query->asArray()->all();
        if (count($data) > $pageSize) {
            unset($data[$pageSize]);
            $nextPage = $page + 1;
        }

        if(!empty($data)){
            foreach ($data  as $k=>$d){
                $data[$k]['create_at'] = date('Y-m-d H:i:s',$d['create_at']);
            }
    }


        return ['nextPage' => $nextPage, 'list' => $data];
    }
}